#pragma once
#include<iostream>
#include<vector>
#include<cstdlib>
#include"AttachCollDetection.h"
#include"time.h"
#include"stdafx.h"
#include"Reader.h"
#include"ColourRamp\Colour.h"
#include"TriangleMesh.h"
#include"EdgeList.h"
#include"Parameters.h"
#include"DistanceField.h"
#include"helpers.h"


using namespace std;

/* STATIC */
static float*		sourceGeom; // Source mesh
static int*			sourceTri;
static float*		targetGeom; // Target mesh
static int*			targetTri;
static TriangleMesh source;
static TriangleMesh target;
static Parameters	params;

/* MAIN */
int main() {
    try {	
		//char endc; cin >> endc;
		Reader *reader = new Reader();

		// Load meshes
		TriangleMesh source = reader->loadObjFile("Input/Sphere/s2.obj");	
		sourceTri = new int[source.getNumFace()*3];
		sourceTri = vector2array(source.getFaces(), sourceTri);
		sourceGeom = new float[source.getNumVert()*3];
		sourceGeom = vector2array(source.getVertices(), sourceGeom);
		TriangleMesh target = reader->loadObjFile("Input/Sphere/s1.obj");
		targetTri = new int[target.getNumFace()*3];
		targetTri = vector2array(target.getFaces(), targetTri);
		targetGeom = new float[target.getNumVert()*3];
		targetGeom = vector2array(target.getVertices(), targetGeom);		
		params.num_vert_ = source.getNumVert();
		params.num_face_ = source.getNumFace();
		params.targetNumTri = target.getNumFace();
		params.targetNumV = target.getNumVert();
		source.buildEdgeList();
		target.buildEdgeList();		
       
		double start = clock();		// Start clock time
		
		MeshStruct* src = new MeshStruct(sourceGeom, sourceTri, 3 * params.num_vert_, 3 * params.num_face_);
		MeshStruct* trg = new MeshStruct(targetGeom, targetTri, 3 * target.getNumVert(), 3 * target.getNumFace());

		for(int i = 7; i <= 7; i++)
		{
		int octreeDepth = i;
		int coarseOctreeDepth = 1;

		DistanceField* distfield = new DistanceField(octreeDepth, coarseOctreeDepth, src, trg);
		distfield->init();

		double finish = clock(); // Finish time	

		/* BENCHMARK */
		cout << endl << "Number of fine voxels: " << pow(8,octreeDepth) << endl;
		cout << "OVERALL Elapsed time: " << (finish - start)/CLOCKS_PER_SEC <<"sec" << endl;		
		//char endc; cin >> endc;
		delete distfield;
		}

		/* CLEANUP */
		delete src;
		delete trg; 
		delete[] sourceGeom;
		delete[] sourceTri;
		delete[] targetTri;
		delete[] targetGeom;
		delete reader;
		return 0;		
    }
    catch (std::exception& e) {
        cout << e.what() << endl;
		return -1;
    }
}

